package dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import dao.impl.DBLoader;
import dao.impl.StringLoader;
import dao.models.Answer;
import dao.models.Chapter;
import dao.models.Question;
import dao.models.Session;

public class DAO implements ILoader {
    private static final DAO _INSTANCE = new DAO();
    private Map<String, Session> sessions = new HashMap<String, Session>();
    private Random random = new Random();

    public static DAO getInstance() {
	return _INSTANCE;
    }

    private ILoader loader = new DBLoader();

    @Override
    public List<Chapter> getChapters() {
	return loader.getChapters();
    }

    @Override
    public Chapter getChapter(String id) {
	return loader.getChapter(id);
    }

    /**
     * Returns id of new session
     * 
     * @param chapters
     *            List of chapters that participate test.
     */
    public String startNewSession(List<Chapter> chapters) {
	String id = String.valueOf(random.nextLong());
	sessions.put(id, new Session(chapters));
	return id;
    }

    public Session getSessionById(String id) {
	return sessions.get(id);
    }

    public Answer getAnswerById(String answerId) {
	return loader.getAnswerById(answerId);
    }

    @Override
    public boolean addNewQuestionToChapter(String chapterId, Question q) {
	return loader.addNewQuestionToChapter(chapterId, q);
    }

    @Override
    public boolean addNewChapter(Chapter chapter) {
	return loader.addNewChapter(chapter);
    }

}
